/**   
 * 项目名：五位一体化工安全信息平台
 * 日期：    2019-12-03 14:14:47  
 * Copyright (c) 2015- joysuch-版权所有     
 */
package com.joysuch.wwyt.bp.controller;

import com.joysuch.wwyt.bp.bean.BpStorageTankAddBean;
import com.joysuch.wwyt.bp.bean.BpStorageTankBean;
import com.joysuch.wwyt.bp.entity.BpStorageTank;
import com.joysuch.wwyt.bp.service.BpStorageTankService;
import com.joysuch.wwyt.core.common.ajax.ResultBean;
import com.joysuch.wwyt.core.common.util.PageDataUtils;
import io.swagger.annotations.Api;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@RestController
@Api(tags = "设备设施管理-储罐")
@RequestMapping("/bp/tank")
public class BpStorageTankController {

    private static final Logger logger = LoggerFactory.getLogger(BpStorageTankController.class);

    @Autowired
    private BpStorageTankService bpStorageTankService;


    @GetMapping("/list")
    public ResultBean list(Pageable page){
        page = PageDataUtils.addOrderByDesc(page, "id");
        Page<BpStorageTank> list = bpStorageTankService.findByPage(page);
        return ResultBean.pageData(list.getContent(), list.getTotalElements());
    }

    @PostMapping("/add")
    public ResultBean add(@RequestBody @Valid BpStorageTankAddBean bpStorageTankAddBean) throws Exception {
        String errorMessage =bpStorageTankService.add(bpStorageTankAddBean);
        if (StringUtils.isNotBlank(errorMessage)) {     
            throw new IllegalArgumentException(errorMessage);
        }
        return ResultBean.defaultSuccessResult();
    }

    @PostMapping("/update")
    public ResultBean update(@RequestBody @Valid BpStorageTankAddBean bpStorageTankAddBean) throws Exception {
        String errorMessage =bpStorageTankService.update(bpStorageTankAddBean);
        if (StringUtils.isNotBlank(errorMessage)) {
            throw new IllegalArgumentException(errorMessage);
        }
        return ResultBean.defaultSuccessResult();
    }

    @PostMapping("/delete")
    public ResultBean delete(Long id) throws Exception {
        bpStorageTankService.delete(id);
        return ResultBean.defaultSuccessResult();
    }

    @GetMapping("getById")
    public ResultBean getById(Long id){
        BpStorageTankBean byId = bpStorageTankService.findById(id);
        return ResultBean.wrapSuccessfulResult(byId);
    }


}
